//
//  TrainViewController.m
//  ZHIFUBAO
//
//  Created by 高新川 on 15/6/24.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "TrainViewController.h"
#import "AFNetworking.h"
#import "AFURLRequestSerialization.h"
#import "TicketsViewController.h"

@interface TrainViewController ()

@end

@implementation TrainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.trainTitle.layer.cornerRadius = 15;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)searchTickets:(id)sender {
    NSString *start = self.startCity.text;              // 参数一
    NSString *end = self.endCity.text;                  // 参数二
    //NSString *str=[NSString stringWithFormat:@"http://apis.juhe.cn/train/yp?key=33dafa6f240ec84c797b423df462e5d1&from=%@&to=%@",start,end];
    
    NSString *str=[NSString stringWithFormat:@"http://apis.juhe.cn/train/s2swithprice?start=%@&end=%@&key=33dafa6f240ec84c797b423df462e5d1", start, end]; // URL
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];                        // UTF－8
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    // 请求数据，设置成功与失败的回调函数
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dict=[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];
        
        TicketsViewController *controll = [[TicketsViewController alloc] init];
        //NSLog(@"%@",dict);
        controll.data = dict;
        [self.navigationController pushViewController:controll animated:YES];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"发生错误！%@",error);
    }];
    // 加入队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
}

- (IBAction)exchange:(id)sender {
    NSString *temp = self.startCity.text;
    self.startCity.text = self.endCity.text;
    self.endCity.text = temp;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
